package com.iflytek.esb.service;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iflytek.esb.dao.QueueDao;
import com.iflytek.esb.domain.Id;
import com.iflytek.esb.domain.Queue;

@Service
public class QueueService {
	
	@Autowired
	private QueueDao queueDao;
	
	public Map<String, Object> deQueue() {
		
		Queue q = queueDao.getQueue(new Date().getTime());
		if (q != null) {
			queueDao.deleteQueue(q.getId());
			return q.getData();
		}
		else {
			return null;
		}
	}
	
	public void inQueue(Map<String, Object> data, Date dueTime) {
		Id id = new Id();
		queueDao.newQueueId(id);
		
		Queue q = new Queue();
		q.setId(id.getMaxId());
		q.setData(data);
		q.setDueTime(dueTime.getTime());
		q.setInTime(new Date().getTime());
		
		queueDao.insertQueue(q);
	}

}
